<?php

declare(strict_types=1);

namespace App\AdminApi\Modular\Data\Controller;

use App\AdminApi\BasicsController;
use App\AdminApi\Modular\Data\Service\BannerService;
use App\AdminApi\Modular\Data\Validate\BannerValidate;
use App\Common\Extend\Guard\Auth;
use Core\HyperfUnit\Response;
use Core\Package\ElementPlus\FormCreate\ElemenuForm;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\{Controller, GetMapping, PostMapping, RequestMapping};
use Psr\Http\Message\ResponseInterface;
use Zms\HyperfValidateAnnotations\Validate;

#[
    Auth(guard: self::GUARD, label: ['数据管理', '轮播'], app: self::APP),
    Controller(prefix: "/admin-api/data-banner", server: self::SERVER)
]
class BannerController extends BasicsController
{

    #[Inject]
    protected BannerService $service;

    #[
        GetMapping('classify'),
        Auth(label: ['轮播分类'], relyon: ["data.banner.list"], logger: false)
    ]
    public function classify(): ResponseInterface
    {
        return Response::Api()->success(data: $this->service->getClassify());
    }

    #[
        GetMapping('list'),
        Auth(sign: "data.banner.list", view: '/data/banner/index')
    ]
    public function list(): ResponseInterface
    {
        $where = $this->request->inputs(['position','title','status']);
        return Response::Api()->success(data: $this->service->search($where)->paginate($this->pageSize()));
    }


    #[
        RequestMapping('created'),
        Auth(sign: "data.banner.created"),
        ElemenuForm(class: BannerService::class, function: 'createdBannerForm'),
        Validate(class: BannerValidate::class, scene: 'created')
    ]
    public function created(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->created($data)) {
            return Response::Api()->success('轮播添加成功');
        }
        return Response::Api()->fail('轮播添加失败');
    }


    #[
        RequestMapping('updated'),
        Auth(sign: "data.banner.updated"),
        ElemenuForm(class: BannerService::class, function: 'updatedBannerForm', param: ['id']),
        Validate(class: BannerValidate::class, scene: 'updated')
    ]
    public function updated(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->updated($data)) {
            return Response::Api()->success('轮播编辑成功');
        }
        return Response::Api()->fail('轮播编辑失败');
    }


    #[
        PostMapping('status'),
        Auth(label: ['修改轮播状态'], relyon: ["data.banner.updated"]),
        Validate(rules: [
            ['id', 'required', 'error' => '请选择要更改的内容'],
            ['status', 'in', 'enums' => ['0', '1'], 'error' => '数据异常,请刷新重试']
        ])
    ]
    public function status(): ResponseInterface
    {
        $data = $this->request->inputs(['id', 'status']);
        if ($this->service->search(['id' => $data['id']])->update(['status' => $data['status']])) {
            return Response::Api()->success('更改成功');
        }
        return Response::Api()->fail('更改失败');
    }


    #[
        PostMapping('delete'),
        Auth(sign: "data.banner.delete"),
        Validate(rules: [['id', 'required', 'error' => '请选择要删除的内容']])
    ]
    public function delete(): ResponseInterface
    {
        $id = $this->request->input('id', 0);
        if ($this->service->search(['id' => $id])->delete()) {
            return Response::Api()->success('删除成功');
        }
        return Response::Api()->fail('删除失败');
    }
}